from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_list
from django.views.generic.create_update import create_object, update_object, delete_object
from to2.models import Locomotive, LocJournal

def generic_wrapper_journal(request, locomotive_id):
    '''This wrapper applies filter on QuerySet object, which will be supplied'''
    qs = LocJournal.objects.filter(locomotive=locomotive_id)
    ex_cont = {'loc':Locomotive.objects.get(id=locomotive_id)}
    return object_list(request, queryset=qs, allow_empty=True, extra_context = ex_cont)

def generic_wrapper_journal_add(request, locomotive_id):
	ex_cont = {'loc':Locomotive.objects.get(id=locomotive_id)}
	return create_object(request, model=LocJournal,
		post_save_redirect='/locomotive/%s/journal' % (locomotive_id,), extra_context = ex_cont)\
		
def generic_wrapper_journal_update(request, locomotive_id, object_id):
	ex_cont = {'loc':Locomotive.objects.get(id=locomotive_id)}
	return update_object(request,
		model=LocJournal,
		object_id = object_id,
		post_save_redirect='/locomotive/%s/journal' % (locomotive_id,),
		extra_context = ex_cont)

def generic_wrapper_journal_delete(request, locomotive_id, object_id):
	return delete_object(request,
		model=LocJournal,
		object_id = object_id,
		post_delete_redirect='/locomotive/%s/journal' % (locomotive_id,))

urlpatterns = patterns('',
    url(r'^$',generic_wrapper_journal,{},name='locomotive_journal'),
    url(r'^add/$',generic_wrapper_journal_add,{},name='locjournal_add'),
    url(r'^(?P<object_id>[0-9]*)/update/$',generic_wrapper_journal_update,{}, name='locjournal_update'),
    url(r'^(?P<object_id>[0-9]*)/delete/$',generic_wrapper_journal_delete,{},name='locjournal_delete')
)
